package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.readRkc
import com.hontech.libratest.conn.writeRkc
import com.hontech.libratest.utils.*

class RkcSub (view: View) {

    private val etReg = view.findViewById<EditText>(R.id.id_rkc_et_reg_addr)
    private val etVal = view.findViewById<EditText>(R.id.id_rkc_et_reg_val)
    private val btnRead = view.findViewById<Button>(R.id.id_rkc_btn_read)
    private val btnWrite = view.findViewById<Button>(R.id.id_rkc_btn_write)

    private val btnCoolStartSet = view.findViewById<Button>(R.id.id_rkc_btn_cool_start_set)
    private val btnCoolStopSet = view.findViewById<Button>(R.id.id_rkc_btn_cool_stop_set)
    private val btnCoolGet = view.findViewById<Button>(R.id.id_rkc_btn_cool_get)

    private val etStart = view.findViewById<EditText>(R.id.id_rkc_et_cool_start)
    private val etStop = view.findViewById<EditText>(R.id.id_rkc_et_cool_stop)

    init {
        btnRead.onClick(::onClickRead)
        btnWrite.onClick(::onClickWrite)
        btnCoolStartSet.onClick(::onClickSetCoolStart)
        btnCoolStopSet.onClick(::onClickSetCoolStop)
        btnCoolGet.onClick(::onClickCoolGet)
    }

    private suspend fun onClickSetCoolStart() {
        val start = etStart.toInt() * 10
        val w = showProg(btnRead, "设置制冷开机温度")
        try {
            writeRkc(0x0400, start.unsignedInt16())
            w.success("设置成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickSetCoolStop() {
        val start = etStop.toInt() * 10
        val w = showProg(btnRead, "设置制冷停机温度")
        try {
            writeRkc(0x0401, start.unsignedInt16())
            w.success("设置成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickCoolGet() {
        val w = showProg(btnRead, "查询制冷温度")
        try {
            val start = readRkc(0x0400)
            val stop = readRkc(0x0401)
            w.success("开机温度:${start.signedInt16()/10} 停机温度:${stop.signedInt16()/10}")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickRead() {
        val reg = etReg.toInt(16)
        val w = showProg(btnRead, "读取温控器")
        try {
            val v = readRkc(reg)
            w.success("读取:${v}")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickWrite() {
        val reg = etReg.toInt(16)
        val value = etVal.toInt(16)
        val w = showProg(btnRead, "写入温控器")
        try {
            writeRkc(reg, value)
            w.success("写入成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }
}

